//
//  ReadListViewController.m
//  WYnewsss
//
//  Created by lanou on 16/4/19.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadListViewController.h"
#import "ReadListViewController.h"
#import "ReadListModel.h"
#import "FactoryTableViewCell.h"
#import "BaseTableViewCell.h"

#import "NetWorkRequestManager.h"
//#import "ReadListImgModel.h"
#import <MJRefresh.h>
#import "ReadMyDetailViewController.h"
#import "ReadDetailViewController.h"
@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSInteger sortType;
}
//@property (weak, nonatomic) IBOutlet UIScrollView *rootScrollView;
@property (nonatomic,strong) UISegmentedControl *segment;

@property (weak, nonatomic) IBOutlet UITableView *readTableView;
//@property (weak, nonatomic) IBOutlet UITableView *myListTableView;
@property(nonatomic,strong)NSMutableArray *readArray;
@property(nonatomic,strong)NSMutableArray *myListArray;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)NSInteger size;

@end

@implementation ReadListViewController
-(NSMutableArray *)readArray {
    if (!_readArray) {
        self.readArray =[NSMutableArray array];
    }
    return _readArray;
}

-(void)requsetData{
    [NetWorkRequestManager requestWithType:GET urlString:@"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&passport=&devId=QRiIw%2FNTxZ%2F6lRKi5Sz77gXKYEnWfW3PkS84r4IywqlO71uoInTske2%2Fjk0rCzdE&size=20&version=6.0&spever=false&net=wifi&lat=&lon=&ts=1460989102&sign=4JIWLSBYvOpWMSuo6hfx7ux8IPJL6K2ypC6FQRe%2FmGJ48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstore" parDic:@{@"count":@(_count),@"size":[NSString stringWithFormat:@"%ld",self.size+=20]} finish:^(NSData *data) {
        [self.readArray removeAllObjects];
        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        // NSLog(@"%@",dataDic);
        NSArray *array=dataDic[@"推荐"];
        for (NSDictionary *dic in array) {
            ReadListModel *model =[[ReadListModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            [self.readArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_readTableView reloadData];
            [self.readTableView.mj_header endRefreshing];
            [self.readTableView.mj_footer endRefreshing];
        });
        
    } error:^(NSError *error) {
        
    }];
}

//-(void)requestDataWithSort{
//  [NetWorkrequestManage requestWithType:GET url:@"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&passport=&devId=QRiIw%2FNTxZ%2F6lRKi5Sz77gXKYEnWfW3PkS84r4IywqlO71uoInTske2%2Fjk0rCzdE&size=20&version=6.0&spever=false&net=wifi&lat=&lon=&ts=1460989102&sign=4JIWLSBYvOpWMSuo6hfx7ux8IPJL6K2ypC6FQRe%2FmGJ48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstore" parameters:@{@"count":@(_count),@"size":[NSString stringWithFormat:@"%ld",self.size+=20]} finish:^(NSData *data) {
//      [self.readArray removeAllObjects];
//    NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//     // NSLog(@"%@",dataDic);
//                NSArray *array=dataDic[@"推荐"];
//                for (NSDictionary *dic in array) {
//        ReadListModel *model =[[ReadListModel alloc]init];
//                    
//    [model setValuesForKeysWithDictionary:dic];
//        [self.readArray addObject:model];
//}
//        dispatch_async(dispatch_get_main_queue(), ^{
//                           
//            [_readTableView reloadData];
//            [self.readTableView.mj_header endRefreshing];
//            [self.readTableView.mj_footer endRefreshing];
//          });
//                    
//                    
//        } error:^(NSError *error) {
//                    
//                    
//    }];
//}
//



- (void)viewDidLoad {
    [super viewDidLoad];
   
    _count=0;
    _size=20;
    //[self requestDataWithSort];
    [self requsetData];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    [_readTableView registerNib:[UINib nibWithNibName:@"ReadListViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadListModel class])];
    
    
    self.readTableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(bottomDragRefreshData)];
    self.readTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(topDragRefreshData)];
    [self.readTableView.mj_header beginRefreshing];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)bottomDragRefreshData{
    [self requsetData];
    
}
-(void)topDragRefreshData{
    [self requsetData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadListModel *model =_readArray[indexPath.row];
  
    
            if (model.imgnewextra.count==2) {
                return 200;
            }else{
                return 150;
            }
    }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 根据标识来确定数据源
   
  
     return self.readArray.count;
    
       
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ReadListModel *model =_readArray[indexPath.row];

    
    
    BaseTableViewCell *cell =nil;
    
    if (model.imgnewextra.count==2) {

        cell =[tableView dequeueReusableCellWithIdentifier:@"ReadListImgCell"];
        if (!cell) {
            
            cell =[[NSBundle mainBundle]loadNibNamed:@"ReadListImgCell" owner:nil options:nil].lastObject;
           
        }
    }else{
        
        cell=[tableView dequeueReusableCellWithIdentifier:@"ReadListViewCell"];
        if (!cell) {
            cell=[[NSBundle mainBundle]loadNibNamed:@"ReadListViewCell" owner:nil
                                            options:nil].lastObject;
            
        }
    }
    
    
    [cell setDataWithModel:model];
    
    return cell;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        ReadListModel *model =_readArray[indexPath.row];
        ReadDetailViewController *detailVC=[[ReadDetailViewController alloc]init];
        detailVC.docid =model.docid;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
